<template>
	<div class="path-list container">
		<Header>
			<span v-if="pathStatus">添加地址</span>
			<span v-else>编辑地址</span>
		</Header>
		<section>
			<van-address-edit
			  v-if="pathStatus"
			  :area-list="areaList"
			  show-set-default
			  @save="onAdd"
			/>
			
			<van-address-edit
			  v-else
			   :address-info="AddressInfo"
			  :area-list="areaList"
			  show-delete
			  show-set-default
			  show-search-result
			  @save="onUpdata"
			  @delete="onDelete"
			/>

		</section>
		<Tabbar></Tabbar>
	</div>
</template>

<script>
	import { Toast } from 'vant';
	import Header from "@/components/path/Header.vue"
	import Tabbar from "@/components/common/Tabbar.vue"
	import http from '@/common/api/request.js'
	export default{
		name: "Path-list",
		components:{
		   Header,
		   Tabbar
		},
		data() {
		    return {
			  id:null,
			  pathStatus:false,
		      areaList:{
				   province_list: {
				      110000: '北京市',
				      120000: '天津市',
				    },
				    city_list: {
				      110100: '北京市',
				      120100: '天津市',
				    },
				    county_list: {
				      110101: '东城区',
				      110102: '西城区',
				      // ....
				    },
			  },
		      // searchResult: [],
		    };
		},
		created() {
			let key = JSON.parse( JSON.stringify(this.$route.params.key))		
			
			//是通过添加进来的
			if( key == "add" ){//通过添加点击进来的
				//修改状态
				this.pathStatus = true;
			}else{
				//编辑进来的
				this.AddressInfo = key
				this.AddressInfo.isDefault = this.AddressInfo.isDefault==1? true : false
			}
			
		},
		methods:{
			//点击保存触发 ==>增加
		    onAdd(content) {
				content.isDefault = content.isDefault==true? 1:0
				http.$axios({
					url:'/api/AddAddress',
					method:'post',
					headers:{
						token:true
					},
					data:{
						...content
					},
				}).then(res=>{
					
					if(!res.success) return
					Toast(res.msg);
					
					this.$router.push('/path')
				})
			  
			},
			
			//点击保存触发 ==>修改
			onUpdata(content){
				content.isDefault = content.isDefault==true? 1:0
				http.$axios({
					url:'/api/UpdataAddress',
					method:'post',
					headers:{
						token:true
					},
					data:{
						...content
					},
				}).then(res=>{
					console.log(res);
					if(!res.success) return
					Toast(res.msg);
					
					this.$router.push('/path')
				})
				// Toast("Updata")
			},
			
			//删除内容
			 onDelete(content) {
				 console.log(content.id)
			     http.$axios({
			     	url:'/api/DeleteAddress',
			     	method:'post',
			     	headers:{
			     		token:true
			     	},
			     	data:{
						id:content.id
			     	},
			     }).then(res=>{
			     	console.log(res);
			     	if(!res.success) return
			     	Toast(res.msg);
			     	
			     	this.$router.push('/path')
			     })
			 },
			
		  },
	}
</script>

<style scoped lang="less">
	.path-list{
		width: 100vw;
		section{
			background-color: #f7f7f7;
			
			.van-address-edit{
				padding:0;
			}
			/deep/.van-address-edit__buttons{
				display: flex;
				justify-content: center;
				align-items: center;
				flex-wrap: wrap;
			}
			
		    /deep/.van-button--danger{
				width: 8rem;
				color:#fff;
			    border:1px solid #b0352f;
			    background-color: #b0352f;
		    }
			
			/deep/.van-button--default{
				width: 8rem;
				// color:#fff;
			    border:1px solid #999;
			    background-color: #fff;
			}
		}
		
		/deep/.tabbar{
			border-top:2px solid #ccc;
		}
	}
	
</style>